package com.qianfeng.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.pojo.Car;
import com.qianfeng.service.CarService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("*")
public class CarController {
    @Resource(type = CarService.class)
    private CarService carService;
    @GetMapping("/car")
    public Map<String,Object> selectAll(){
        List<Car> list = carService.selectAll();
        Map<String,Object>map=new HashMap<>();
        map.put("carData",list);
        map.put("carTotal",list.size());
        return map;
    }
    @PostMapping("/car")
    public String insert(@RequestBody Car car){
        int row = carService.insertSelective(car);
        return row==1?"msgok":"msgno";
    }
    @GetMapping("/carSelect")
    public Map<String,Object> select(String carid,String carzy,String ename,Integer shiyongzhuangtaiid){
        List<Car> list=carService.select(carid,carzy,ename,shiyongzhuangtaiid);
        //分页后获取当前页的数据集合
        Map<String,Object> map=new HashMap<>();
        map.put("carData",list);
        map.put("carTotal",list.size());
        return map;
    }
    @GetMapping("/selectByCarid")
    public Car selectByCarid(String carid){
        return carService.selectByPrimaryKey(carid);
    }
    @PutMapping("/updateCar")
    public String updateCar(@RequestBody Car car){
        int result = carService.updateByPrimaryKeySelective(car);
        if (result==1){
            return "success";
        }else {
            return "failed";
        }
    }
    @PostMapping("/delete")
    public String deleteCar(String carid){
        int result = carService.updateCar(carid);
        if (result==1){
            return "success";
        }else {
            return "failed";
        }
    }
}
